package com.sdehualu.yinfo.controller;

import com.google.gson.Gson;
import com.sdehualu.yinfo.entity.KkxxKey;
import com.sdehualu.yinfo.entity.KkxxUser;
import com.sdehualu.yinfo.entity.SxxxKey;
import com.sdehualu.yinfo.entity.SxxxUser;
import com.sdehualu.yinfo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/yinfo")
public class Controllers {

    @Autowired
    private Gson gson;


    @Autowired
    private UserService userService;


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String addinfo(@RequestBody String json) {


//        String json1 = json;

//        String time = dtime + " " + ttime;

        System.out.println("*****json**********:");

        System.out.println(json);

//        KkxxUser kkxxUser=null;

        KkxxUser kkxxUser = null;
        try {
            kkxxUser = gson.fromJson(json, KkxxUser.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据格式不合法");

            return "数据格式不合法";
        }
        String kid = kkxxUser.getKkbh();
        String xzqh = kkxxUser.getXzqh();
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****卡口编号不合法**********:");


            System.out.println("*****卡口编号**********:");

            System.out.println(kid);


            return "卡口编号不合法";
        }

        if (xzqh.equals("") || xzqh == null || xzqh.equals(" ")) {

            System.out.println("*****行政区划不合法**********:");


            System.out.println("*****行政区划**********:");

            System.out.println(xzqh);


            return "行政区划不合法";
        }
//        KkxxKey kkxxKey = new KkxxKey();
//        kkxxKey.setKkbh(kid);
//        kkxxKey.setXzqh(xzqh);
        boolean exfl = userService.isExist(kid);
        if (exfl) {
            System.out.println("该卡口编号已存在");

            return "该卡口编号已存在";
        }

        String rtf = userService.addkkxxinfo(kkxxUser);

        System.out.println(rtf);

        return rtf;
    }


    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public String delinfo(String kkbh, String kkmc, String xzqh) {


        String dtid = kkbh;
        String dkk = kkmc;
        String dqh = xzqh;
        System.out.println("*****dtid**********:" + dtid);
        System.out.println("*****dkk**********:" + dkk);
        System.out.println("*****dqh**********:" + dqh);

        String kid = kkbh;
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****卡口编号不合法**********:");


            System.out.println("*****卡口编号**********:");

            System.out.println(kid);


            return "卡口编号不合法";
        }
        if (xzqh.equals("") || xzqh == null || xzqh.equals(" ")) {

            System.out.println("*****行政区划不合法**********:");


            System.out.println("*****行政区划**********:");

            System.out.println(xzqh);


            return "行政区划不合法";
        }

//        KkxxKey kkxxKey = new KkxxKey();
//        kkxxKey.setKkbh(kkbh);
//        kkxxKey.setXzqh(xzqh);


        boolean exfl = userService.isExist(kkbh);
        if (!exfl) {
            System.out.println("该卡口编号不存在");

            return "该卡口编号不存在";
        }

        String rtf = userService.delkkxxinfo(kkbh);
        System.out.println(rtf);

        return rtf;

    }

    @RequestMapping(value = "/upinfo", method = RequestMethod.POST)
    @ResponseBody
    public String upinfo(@RequestBody String json) {


//        String json1 = json;

//        String time = dtime + " " + ttime;

        System.out.println("*****json**********:");

        System.out.println(json);

//        KkxxUser kkxxUser=null;

        KkxxUser kkxxUser = null;
        try {
            kkxxUser = gson.fromJson(json, KkxxUser.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据格式不合法");

            return "数据格式不合法";
        }
        String kid = kkxxUser.getKkbh();
        String xzqh = kkxxUser.getXzqh();
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****卡口编号不合法**********:");


            System.out.println("*****卡口编号**********:");

            System.out.println(kid);


            return "卡口编号不合法";
        }

        if (xzqh.equals("") || xzqh == null || xzqh.equals(" ")) {

            System.out.println("*****行政区划不合法**********:");


            System.out.println("*****行政区划**********:");

            System.out.println(xzqh);


            return "行政区划不合法";
        }
//        KkxxKey kkxxKey = new KkxxKey();
//        kkxxKey.setKkbh(kid);
//        kkxxKey.setXzqh(xzqh);
//
        String kkxxKey=kid;

        boolean exfl = userService.isExist(kkxxKey);
        if (!exfl) {
            System.out.println("该卡口编号不存在");

            return "该卡口编号不存在";
        }



        String rtf = userService.delkkxxinfo(kkxxKey);

        String rtf2 = userService.addkkxxinfo(kkxxUser);

        System.out.println(rtf);
        System.out.println(rtf2);

        String uprt="";


        if (rtf.equals("success")&&rtf2.equals("success")){

            uprt="success";
        }else {
            uprt="修改失败请重试";

        }

        return uprt;
    }


    @RequestMapping(value = "/upsxinfo", method = RequestMethod.POST)
    @ResponseBody
    public String upsxinfo(@RequestBody String json) {


//        String json1 = json;

//        String time = dtime + " " + ttime;

        System.out.println("*****json**********:");

        System.out.println(json);

//        KkxxUser kkxxUser=null;

        SxxxUser sxxxUser = null;
        try {
            sxxxUser = gson.fromJson(json, SxxxUser.class);
        } catch (Exception e) {
            e.printStackTrace();
            return "数据格式不合法";
        }
        String kid = sxxxUser.getSxjbh();
        String strdwbh = sxxxUser.getDwxh();
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****摄像机编号不合法**********:");


            System.out.println("*****摄像机编号不合法**********:");

            System.out.println(kid);


            return "摄像机编号不合法";
        }

        if (strdwbh.equals("") || strdwbh == null || strdwbh.equals(" ")) {

            System.out.println("*****点位序号不合法**********:");


            System.out.println("*****点位序号不合法**********:");

            System.out.println(strdwbh);


            return "点位序号不合法";
        }


//        SxxxKey sxxxKey = new SxxxKey();
//        sxxxKey.setSxjbh(kid);
//        sxxxKey.setDwxh(strdwbh);

        String sxxxKey=kid;

        boolean exfl = userService.isExtsx(sxxxKey);
        if (!exfl) {
            System.out.println("摄像机编号不存在");

            return "摄像机编号不存在";
        }
        String rtf = userService.delsxxxinfo(sxxxKey);

        String rtf2 = userService.addsxxxinfo(sxxxUser);
        String uprt="";


        if (rtf.equals("success")&&rtf2.equals("success")){

            uprt="success";
        }else {
            uprt="修改失败请重试";

        }

        return uprt;
    }


    @RequestMapping(value = "/addsx", method = RequestMethod.POST)
    @ResponseBody
    public String addsxinfo(@RequestBody String json) {


//        String json1 = json;

//        String time = dtime + " " + ttime;

        System.out.println("*****json**********:");

        System.out.println(json);

//        KkxxUser kkxxUser=null;

        SxxxUser sxxxUser = null;
        try {
            sxxxUser = gson.fromJson(json, SxxxUser.class);
        } catch (Exception e) {
            e.printStackTrace();
            return "数据格式不合法";
        }
        String kid = sxxxUser.getSxjbh();
        String strdwbh = sxxxUser.getDwxh();
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****摄像机编号不合法**********:");


            System.out.println("*****摄像机编号不合法**********:");

            System.out.println(kid);


            return "摄像机编号不合法";
        }

        if (strdwbh.equals("") || strdwbh == null || strdwbh.equals(" ")) {

            System.out.println("*****点位序号不合法**********:");


            System.out.println("*****点位序号不合法**********:");

            System.out.println(strdwbh);


            return "点位序号不合法";
        }


//        SxxxKey sxxxKey = new SxxxKey();
//        sxxxKey.setSxjbh(kid);
//        sxxxKey.setDwxh(strdwbh);
        String sxxxKey=kid;

        boolean exfl = userService.isExtsx(sxxxKey);
        if (exfl) {
            System.out.println("摄像机编号已存在");

            return "摄像机编号已存在";
        }

        String rtf = userService.addsxxxinfo(sxxxUser);
        System.out.println(rtf);

        return rtf;
    }

    @RequestMapping(value = "/delsx", method = RequestMethod.DELETE)
    @ResponseBody
    public String delsxinfo(String sxjbh, String dwxh) {


        System.out.println("*****sxjbh**********:" + sxjbh);
        System.out.println("*****dwxh**********:" + dwxh);

        String kid = sxjbh;
        String strdwbh = dwxh;
        if (kid.equals("") || kid == null || kid.equals(" ")) {

            System.out.println("*****摄像机编号不合法**********:");


            System.out.println("*****摄像机编号不合法**********:");

            System.out.println(kid);


            return "摄像机编号不合法";
        }

        if (strdwbh.equals("") || strdwbh == null || strdwbh.equals(" ")) {

            System.out.println("*****点位序号不合法**********:");


            System.out.println("*****点位序号不合法**********:");

            System.out.println(strdwbh);


            return "点位序号不合法";
        }

//        SxxxKey sxxxKey = new SxxxKey();
//        sxxxKey.setSxjbh(kid);
//        sxxxKey.setDwxh(strdwbh);
        String sxxxKey=kid;

        boolean exfl = userService.isExtsx(sxxxKey);


        if (!exfl) {
            System.out.println("摄像机编号或点位序号不存在");

            return "摄像机编号或点位序号不存在";
        }

        String rtf = userService.delsxxxinfo(sxxxKey);
        System.out.println(rtf);

        return rtf;

    }







    @RequestMapping(value = "/getallinfo", method = RequestMethod.GET)
    @ResponseBody
    public List<KkxxUser> getallinfo() {
        List<KkxxUser> listkkxxuser = userService.selallinfo();

        return listkkxxuser;
    }


    @RequestMapping(value = "/getsxallinfo", method = RequestMethod.GET)
    @ResponseBody
    public List<SxxxUser> getallsxinfo() {
        List<SxxxUser> listsxxxuser = userService.selallsxinfo();

        return listsxxxuser;
    }

    @RequestMapping(value = "/getbyid", method = RequestMethod.GET)
    @ResponseBody
    public KkxxUser getbyid(String kkbh, String xzqh) {



        KkxxUser kkxxUser = userService.selinfobk(kkbh);

        return kkxxUser;
    }

    @RequestMapping(value = "/getsxbyid", method = RequestMethod.GET)
    @ResponseBody
    public SxxxUser getsxbyid(String sxjbh, String dwxh) {


        SxxxUser sxxxUser = userService.selinfosxbk(sxjbh);


        return sxxxUser;
    }


    /*
    不确定前台根据哪种条件查询，非json格式
    */
    @RequestMapping(value = "/findbyanyone", method = RequestMethod.GET)
    @ResponseBody
    public Page<KkxxUser> findByAny(


            String key,

//            卡口编号
            String kkbh,
//            卡口类型
            String kklx,
//            行政区划
            String xzqh,
//            卡口名称
            String kkmc,
            Integer pageNum, //第几页默认1

            Integer pageSize, //每页几行 默认50

            String sortProperty, //排序字段默认kkbh

            String sortDirection //排序方向 默认DESC
    ) {


        Map<String, String> searchParams = new HashMap<String, String>();

        if (key != null && !"".equals(key)) {
            searchParams.put("key", key);
        }


        if (kkbh != null && !"".equals(kkbh)) {
            searchParams.put("kkbh", kkbh);
        }
        if (kklx != null && !"".equals(kklx)) {
            searchParams.put("kklx", kklx);
        }

        if (xzqh != null && !"".equals(xzqh)) {
            searchParams.put("xzqh", xzqh);
        }

        if (kkmc != null && !"".equals(kkmc)) {
            searchParams.put("kkmc", kkmc);
        }

        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 50;
        }
        if (sortProperty == null || "".equals(sortProperty)) {
            sortProperty = "kkbh";
        }
        if (sortDirection == null || "".equals(sortDirection)) {
            sortDirection = "DESC";
        }


        return userService.findByAnyone(pageNum, pageSize, sortProperty, sortDirection, searchParams);


    }



    /*
    不确定前台根据哪种条件查询，非json格式
    */
    @RequestMapping(value = "/findbyanyonejz", method = RequestMethod.GET)
    @ResponseBody
    public Page<KkxxUser> findbyanyonejz(

//            卡口编号
            String kkbh,
//            卡口类型
            String kklx,
//            行政区划
            String xzqh,
//            卡口名称
            String kkmc,
            Integer pageNum, //第几页默认1

            Integer pageSize, //每页几行 默认50

            String sortProperty, //排序字段默认kkbh

            String sortDirection //排序方向 默认DESC
    ) {


        Map<String, String> searchParams = new HashMap<String, String>();

        if (kkbh != null && !"".equals(kkbh)) {
            searchParams.put("kkbh", kkbh);
        }
        if (kklx != null && !"".equals(kklx)) {
            searchParams.put("kklx", kklx);
        }

        if (xzqh != null && !"".equals(xzqh)) {
            searchParams.put("xzqh", xzqh);
        }

        if (kkmc != null && !"".equals(kkmc)) {
            searchParams.put("kkmc", kkmc);
        }

        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 50;
        }
        if (sortProperty == null || "".equals(sortProperty)) {
            sortProperty = "kkbh";
        }
        if (sortDirection == null || "".equals(sortDirection)) {
            sortDirection = "DESC";
        }


//        return userService.findByAnyone(pageNum, pageSize, sortProperty, sortDirection, searchParams);
        return userService.findByAnyonejz(pageNum, pageSize, sortProperty, sortDirection, searchParams);


    }




    /*
    不确定前台根据哪种条件查询，非json格式
    */
    @RequestMapping(value = "/findsxbyanyone", method = RequestMethod.GET)
    @ResponseBody
    public Page<SxxxUser> findsxByAny(

            String key,

//            摄像机编号
            String sxjbh,
//            点位序号
            String dwxh,
//            摄像机名称
            String sxjmc,
            Integer pageNum, //第几页

            Integer pageSize, //第几行

            String sortProperty, //排序字段

            String sortDirection //排序方向
    ) {


        Map<String, String> searchParams = new HashMap<String, String>();
        if (key != null && !"".equals(key)) {
            searchParams.put("key", key);
        }



        if (sxjbh != null && !"".equals(sxjbh)) {
            searchParams.put("sxjbh", sxjbh);
        }
        if (dwxh != null && !"".equals(dwxh)) {
            searchParams.put("dwxh", dwxh);
        }

        if (sxjmc != null && !"".equals(sxjmc)) {
            searchParams.put("sxjmc", sxjmc);
        }



        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 50;
        }
        if (sortProperty == null || "".equals(sortProperty)) {
            sortProperty = "sxjbh";
        }
        if (sortDirection == null || "".equals(sortDirection)) {
            sortDirection = "DESC";
        }


        return userService.findsxByAnyone(pageNum, pageSize, sortProperty, sortDirection, searchParams);


    }



    /*
    不确定前台根据哪种条件查询，非json格式
    */
    @RequestMapping(value = "/findsxbyanyonejz", method = RequestMethod.GET)
    @ResponseBody
    public Page<SxxxUser> findsxbyanyonejz(
//            摄像机编号
            String sxjbh,
//            点位序号
            String dwxh,
//            摄像机名称
            String sxjmc,
            Integer pageNum, //第几页

            Integer pageSize, //第几行

            String sortProperty, //排序字段

            String sortDirection //排序方向
    ) {


        Map<String, String> searchParams = new HashMap<String, String>();

        if (sxjbh != null && !"".equals(sxjbh)) {
            searchParams.put("sxjbh", sxjbh);
        }
        if (dwxh != null && !"".equals(dwxh)) {
            searchParams.put("dwxh", dwxh);
        }

        if (sxjmc != null && !"".equals(sxjmc)) {
            searchParams.put("sxjmc", sxjmc);
        }



        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 50;
        }
        if (sortProperty == null || "".equals(sortProperty)) {
            sortProperty = "sxjbh";
        }
        if (sortDirection == null || "".equals(sortDirection)) {
            sortDirection = "DESC";
        }


        return userService.findsxByAnyonejz(pageNum, pageSize, sortProperty, sortDirection, searchParams);


    }


}
